<?php
//禁用错误报告
error_reporting(0);
//报告运行时错误
error_reporting(E_ERROR | E_WARNING | E_PARSE);
//报告所有错误
error_reporting(E_ALL);

require('config.php');
		$user_id=$_REQUEST['user_id'];
		$json=$_REQUEST['json'];
		$json=json_decode($json,true);
		//file_put_contents("tsxx.txt", "\r\n".$user_id."\r\n", FILE_APPEND);
		//file_put_contents("tsxx.txt", "\r\n".json_encode($json)."\r\n", FILE_APPEND);
		//echo json_encode("123");
		$sql="select * from `".$oto."_car_session` where `userId`='".$user_id."' LIMIT 1";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$person_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$car_session=unserialize($person_info[0]['car_session']);
		$i=0;
		foreach($car_session as $k=>$v)
		{
			$cart[$i]['goods_id']=$v['goodsId'];//商品id
			$cart[$i]['goods_num']=$v['cnt'];//商品数量
			$cart[$i]['isSelected']=$v['ischk'];//是否选中
			$cart[$i]['goodsImg']=$v['goodsThums'];//商品图片
			$cart[$i]['goodsName']=$v['goodsName'];//商品名字
			$cart[$i]['shopPrice']=$v['shopPrice'];//店铺价格
			$cart[$i]['shopId']=$v['shopId'];//店铺id
			$cart[$i]['shopName']=$v['shopName'];//店铺名字
			$cart[$i]['goods_attr']=0;//属性名字集合
			$cart[$i]['goodss_attr_id']=$k;//属性id集合
			$cart[$i]['shopImg']=0;//店铺头像
			$cart[$i]['goodsStock']=0;//商品库存
			$cart[$i]['shuxing_money']=0;//属性价钱
			$lin_goodsId=$v['goodsId'];
			$sql="SELECT ".$oto."_goods.goodsStock,".$oto."_shops.shopImg FROM ".$oto."_goods,".$oto."_shops where ".$oto."_goods.shopId=".$oto."_shops.shopId and ".$oto."_goods.goodsId='{$lin_goodsId}' LIMIT 1";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$goods_info[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$cart[$i]['shopImg']=$goods_info[$i]['shopImg'];
			$cart[$i]['goodsStock']=$goods_info[$i]['goodsStock'];
				$money=0;
				$goods_attr='';
				$attr_id=$k;
				//file_put_contents("tsxx.txt", "\r\n".$attr_id."\r\n", FILE_APPEND);
				$attr_id=explode("_",$attr_id); 
				$lin=$attr_id;
				$cart[$i]['isGroup']=$attr_id[count($attr_id)-1];//是否为团购商品
				$cart[$i]['isSeckill']=$attr_id[count($attr_id)-2];//是否为秒杀商品
				//file_put_contents("tsxx.txt", "\r\n是否团购:".$cart[$i]['isGroup']."\r\n", FILE_APPEND);
				if(count($attr_id)-3==0) $attr_id=false;
				else
				{
					$attr_id='';
					for($j=1;$j<count($lin)-2;$j++)
					{
						if($j==1)
						{
							$attr_id.=$lin[$j];
						}
						else
						{
							$attr_id.=','.$lin[$j];
						}
					}
				}
				if($attr_id==false || $attr_id=='false')
				{
					$money=0;
					$goods_attr='';
					$cart[$i]['goodss_attr_id2']='';
				}
				else
				{
					$z_attr_money;
					$cart[$i]['goodss_attr_id2']=$attr_id;
					$attr_id_list=explode(',',$attr_id);
					$attr_max=count($attr_id_list);
					for($j=0;$j<$attr_max;$j++)
					{
						$z_attr_id=$attr_id_list[$j];
						$sql="SELECT * FROM ".$oto."_goods_attributes,".$oto."_attributes where id='$z_attr_id' and ".$oto."_goods_attributes.attrId=".$oto."_attributes.attrId";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$z_attr_money[$j]=$row;//将取得的所有数据赋值给person_info数组
						}
						$money+=$z_attr_money[$j]['attrPrice'];
						if($j==0) $goods_attr.=$z_attr_money[$j]['attrName'].":".$z_attr_money[$j]['attrVal'];
						else $goods_attr.=','.$z_attr_money[$j]['attrName'].":".$z_attr_money[$j]['attrVal'];
						if($z_attr_money[$j]['attrStock']<$cart[$i]['goodsStock']) $cart[$i]['goodsStock']=$z_attr_money[$j]['attrStock'];
					}
				}
				$cart[$i]['shuxing_money']=$money;//属性价钱
				$cart[$i]['goods_attr']=$goods_attr;//属性名字集合
			$i++;
		}
		/***上面是把购物车session格式转换成二维json格式**/
		$max=count($cart);
		$max2=count($json);
		for($i=0;$i<count($cart);$i++)//这里是把未选中的购物车商品筛选出去
		{
			if($cart[$i]['isSelected']==0)
			{
				array_splice($cart, $i, 1);
				$i--;
			}
		}
		//获取选择的收货地址
		$address_id=$json[$max2-1]['address_id'];
		$sql="select * from `".$oto."_user_address` where `userId`='".$user_id."' and `addressId`='{$address_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_address[]=$row;//将取得的所有数据赋值给person_info数组
		}
		//验证
		$max=count($cart);
		$pan=0;
		for($i=0;$i<count($json)-1;$i++)
		{
			$shop_id=$json[$i]['shops_id'];
			$ad1=$user_address[0]['areaId1'];
			$ad2=$user_address[0]['areaId2'];
			$ad3=$user_address[0]['areaId3'];
			$ad4=$user_address[0]['communityId'];
			$sql="select * from `".$oto."_shops_communitys` where `shopId`='".$shop_id."' and areaId1='0' and areaId2='0' and areaId3='0' and communityId='0'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$isPs2[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			if(!isset($isPs2[$i]))
			{
				$sql="select * from `".$oto."_shops_communitys` where `shopId`='".$shop_id."' and areaId1='{$ad1}' and areaId2='{$ad2}' and areaId3='{$ad3}' and communityId='{$ad4}'";
				$result=$db->query($sql);
				while($row=$result->fetch_assoc()){
					$isPs[$i]=$row;//将取得的所有数据赋值给person_info数组
				}
				if(!isset($isPs[$i]))
				{
					$pan=1;
					$data[$i]['pan']=1;
					$data[$i]['msg']="不支持该配送地址";
				}
				else
				{
					$data[$i]['pan']=0;
					$data[$i]['msg']=$cart[$i]['goodsName']."可配送该地址";
				}
			}
			else
			{
				$data[$i]['pan']=0;
				$data[$i]['msg']=$cart[$i]['goodsName']."可配送该地址";
			}
		}
		if($pan==1)
		{
			//file_put_contents("tsxx.txt", "\r\n".json_encode($data)."\r\n", FILE_APPEND);
			echo json_encode($data);
			die();
		}
		for($i=0;$i<$max;$i++)
		{
			if($cart[$i]['goodsStock']<$cart[$i]['goods_num'])
			{
				$pan=1;
				$data[$i]['pan']=1;
				$data[$i]['msg']=$cart[$i]['goodsName']."不能大于".$cart[$i]['goodsStock']."件";
			}
			else
			{
				$data[$i]['pan']=0;
				$data[$i]['msg']=$cart[$i]['goodsName']."库存充足可购买";
			}
		}
		if($pan==1)
		{
			//file_put_contents("tsxx.txt", "\r\n".json_encode($data)."\r\n", FILE_APPEND);
			echo json_encode($data);
			die();
		}
		// for($i=0;$i<$max;$i++)
		// {
		// 	if($cart[$i]['isSeckill']=='1' || $cart[$i]['isSeckill']==1)
		// 	{
		// 		$sql="select * from `".$oto."_orders` where `areaId`='".$user_address[$i]['areaId1']."'";
		// 		$result=$db->query($sql);
		// 		while($row=$result->fetch_assoc()){
		// 			$isSeckill_info[$i]=$row;//将取得的所有数据赋值给person_info数组
		// 		}
		// 	}
		// }
		/**********************************************************/
		
		for($i=0;$i<count($user_address);$i++)
		{
			$dizhi='';
			$sql="select * from `".$oto."_areas` where `areaId`='".$user_address[$i]['areaId1']."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$sheng[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$sql="select * from `".$oto."_areas` where `areaId`='".$user_address[$i]['areaId2']."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$shi[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$sql="select * from `".$oto."_areas` where `areaId`='".$user_address[$i]['areaId3']."'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$xian[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			// $sql="select * from `".$oto."_communitys` where `communityId`='".$user_address[$i]['communityId']."'";
			// $result=$db->query($sql);
			// while($row=$result->fetch_assoc()){
			// 	$jiedao[$i]=$row;//将取得的所有数据赋值给person_info数组
			// }
			$dizhi=$sheng[$i]['areaName'].$shi[$i]['areaName'].$xian[$i]['areaName'].$user_address[$i]['address'];
		}

		/*********************************************/
		$orderunique=floor(microtime(true)*1000);//是否同一批下单
		$createTime=date('Y-m-d H:i:s');//下单时间
		$areaId1=$user_address[0]['areaId1'];//省
		$areaId2=$user_address[0]['areaId2'];//市
		$areaId3=$user_address[0]['areaId3'];//区
		$communityId=$jiedao[0]['communityId'];//社区id
		$userAddress=$dizhi;//配送详细地址
		$userId=$user_id;//用户id
		$userName=$user_address[0]['userName'];//收货人名字
		$userPhone=$user_address[0]['userPhone'];//收货人手机号
		$t=0;
		for($i=0;$i<$max;$i++)//下单团购跟秒杀
		{
			//file_put_contents("tsxx.txt", $i."\r\n是否是团购：".$cart[$i]['isGroup']."\r\n", FILE_APPEND);
			if($cart[$i]['isGroup']==1 || $cart[$i]['isGroup']=='1')
			{
				for($j=0;$j<$max2-1;$j++)
				{
					if($json[$j]['shops_id']==$cart[$i]['shopId'])
					{
						$orderStrNo=microtime(true);
						//$orderStrNo=time();
						$sql="INSERT INTO ".$oto."_orderids (`rnd`) VALUES ('{$orderStrNo}')";
						$result=$db->query($sql);
						//$sql="SELECT * FROM ".$oto."_orderids where rnd='{$orderStrNo}'";
						$sql="SELECT * FROM ".$oto."_orderids order by id DESC";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$orderids_info[]=$row;//将取得的所有数据赋值给person_info数组
						}
						$orderNo=$orderids_info[0]['id']."".(fmod($orderids_info[0]['id'],7));//订单号
						$shopId=$json[$j]['shops_id'];//商家id
						$orderStatus='-2';//订单状态
						$totalMoney=$cart[$i]['shopPrice'];//总消费
						$deliverType=$json[$j]['shops_psway'];//配送方式
						$orderScore=$cart[$i]['shopPrice'];//获得积分
						$orderRemarks=$json[$j]['shops_liuyan'];//买家留言
						$requireTime=0;//期望送达时间
						$needPay=$cart[$i]['shopPrice'];//应付金额
						$isGroup='1';//是否团购商品

						
						$goodsId=$cart[$i]['goods_id'];
						$goodsNums=$cart[$i]['goods_num'];
						$goodsPrice=$cart[$i]['shopPrice'];
						$goodsAttrId=$cart[$i]['goodss_attr_id2'];
						$goodsAttrName=$cart[$i]['goods_attr'];
						$goodsName=$cart[$i]['goodsName'];
						$goodsThums=$cart[$i]['goodsImg'];

						$sql="SELECT * FROM ".$oto."_goods_group where shopId='{$shopId}' and goodsId='{$goodsId}'";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$group_info[]=$row;//将取得的所有数据赋值给person_info数组
						}
						$group_id=0;
						if(isset($group_info))
						{
							$group_id=$group_info[0]['id'];
						}
						

						$sql="INSERT INTO ".$oto."_orders (`orderNo`,`areaId1`,`areaId2`,`areaId3`,`shopId`,`orderStatus`,`totalMoney`,`isSelf`,`userId`,`userName`,`communityId`,`userAddress`,`userPhone`,`orderScore`,`orderRemarks`,`createTime`,`orderunique`,`needPay`,`orderType`,`payType`,`isPay`) VALUES ('$orderNo','$areaId1','$areaId2','$areaId3','$shopId','$orderStatus','$totalMoney','$deliverType','$userId','$userName','$communityId','$userAddress','$userPhone','$orderScore','$orderRemarks','$createTime','$orderunique','$needPay','3','1','0')";
            			$result=$db->query($sql);//插入order表
            			//file_put_contents("tsxx.txt", "\r\n".$sql."执行：".$result."\r\n", FILE_APPEND);
            			
            			$sql="select * from `".$oto."_orders` where `orderNo`='".$orderNo."' and `userId`='{$userId}' and `orderunique`='{$orderunique}'";
						$result=$db->query($sql);//查询刚插进去的orderId
						while($row=$result->fetch_assoc()){
							$order[0]=$row;//将取得的所有数据赋值给person_info数组
						}
						$orderId=$order[0]['orderId'];
						$sql="INSERT INTO ".$oto."_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','订单已提交，等待支付','$user_id','0','$createTime')";
            			$result=$db->query($sql);
						$data[$t]['orderId']=$orderId;
						$sql="INSERT INTO ".$oto."_order_reminds (`orderId`,`userId`,`shopId`,`createTime`) VALUES ('$orderId','$userId','$shopId','$createTime')";
            			$result=$db->query($sql);//插入order_reminds表
						$t++;
            			$sql="INSERT INTO ".$oto."_order_goods (`orderId`,`goodsId`,`goodsNums`,`goodsPrice`,`goodsAttrId`,`goodsAttrName`,`goodsName`,`goodsThums`,`goodsGroupId`) VALUES ('$orderId','$goodsId','$goodsNums','$goodsPrice','$goodsAttrId','$goodsAttrName','$goodsName','$goodsThums','$group_id')";
            			$result=$db->query($sql);//插入order对应的order_goods表
						break;
					}
				}
				array_splice($cart, $i, 1);
				$i--;
			}
			else if($cart[$i]['isSeckill']==1 || $cart[$i]['isSeckill']=='1')
			{
				for($j=0;$j<$max2-1;$j++)
				{
					if($json[$j]['shops_id']==$cart[$i]['shopId'])
					{
						$orderStrNo=microtime(true);
						//$orderStrNo=time();
						$sql="INSERT INTO ".$oto."_orderids (`rnd`) VALUES ('{$orderStrNo}')";
						$result=$db->query($sql);
						//$sql="SELECT * FROM ".$oto."_orderids where rnd='{$orderStrNo}'";
						$sql="SELECT * FROM ".$oto."_orderids order by id DESC";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$orderids_info[]=$row;//将取得的所有数据赋值给person_info数组
						}
						$orderNo=$orderids_info[0]['id']."".(fmod($orderids_info[0]['id'],7));//订单号
						$shopId=$json[$j]['shops_id'];//商家id
						$orderStatus='-2';//订单状态
						$totalMoney=$cart[$i]['shopPrice'];//总消费
						$deliverType=$json[$j]['shops_psway'];//配送方式
						$orderScore=$cart[$i]['shopPrice'];//获得积分
						$orderRemarks=$json[$j]['shops_liuyan'];//买家留言
						$requireTime=0;//期望送达时间
						$needPay=$cart[$i]['shopPrice'];//应付金额
						$isSeckill='1';//是否秒杀商品

						
						$goodsId=$cart[$i]['goods_id'];
						$goodsNums=$cart[$i]['goods_num'];
						$goodsPrice=$cart[$i]['shopPrice'];
						$goodsAttrId=$cart[$i]['goodss_attr_id2'];
						$goodsAttrName=$cart[$i]['goods_attr'];
						$goodsName=$cart[$i]['goodsName'];
						$goodsThums=$cart[$i]['goodsImg'];
						$sql="INSERT INTO ".$oto."_orders (`orderNo`,`areaId1`,`areaId2`,`areaId3`,`shopId`,`orderStatus`,`totalMoney`,`isSelf`,`userId`,`userName`,`communityId`,`userAddress`,`userPhone`,`orderScore`,`orderRemarks`,`createTime`,`orderunique`,`needPay`,`orderType`,`payType`,`isPay`) VALUES ('$orderNo','$areaId1','$areaId2','$areaId3','$shopId','$orderStatus','$totalMoney','$deliverType','$userId','$userName','$communityId','$userAddress','$userPhone','$orderScore','$orderRemarks','$createTime','$orderunique','$needPay','2','1','0')";
            			$result=$db->query($sql);//插入order表
            			$sql="select * from `".$oto."_orders` where `orderNo`='".$orderNo."' and `userId`='{$userId}' and `orderunique`='{$orderunique}'";
						$result=$db->query($sql);//查询刚插进去的orderId
						while($row=$result->fetch_assoc()){
							$order[0]=$row;//将取得的所有数据赋值给person_info数组
						}
						$orderId=$order[0]['orderId'];
						$sql="INSERT INTO ".$oto."_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$orderId','订单已提交，等待支付','$user_id','0','$createTime')";
            			$result=$db->query($sql);
						$data[$t]['orderId']=$orderId;
						$sql="INSERT INTO ".$oto."_order_reminds (`orderId`,`userId`,`shopId`,`createTime`) VALUES ('$orderId','$userId','$shopId','$createTime')";
            			$result=$db->query($sql);//插入order_reminds表
						$t++;
            			$sql="INSERT INTO ".$oto."_order_goods (`orderId`,`goodsId`,`goodsNums`,`goodsPrice`,`goodsAttrId`,`goodsAttrName`,`goodsName`,`goodsThums`,`goodsGroupId`,`isSeckillGood`) VALUES ('$orderId','$goodsId','$goodsNums','$goodsPrice','$goodsAttrId','$goodsAttrName','$goodsName','$goodsThums','0','1')";
            			$result=$db->query($sql);//插入order对应的order_goods表
						break;
					}
				}
				array_splice($cart, $i, 1);
				$i--;
			}
		}
		for($i=0;$i<count($json)-1;$i++)//检索商家id是否被团购秒杀用完了
		{
			$pan2=0;
			for($j=0;$j<count($cart);$j++)
			{
				if($cart[$j]['shopId']==$json[$i]['shops_id']) $pan2=1;
			}
			if($pan2==0)
			{
				array_splice($json, $i, 1);
				$i--;
			}
		}
		for($i=0;$i<count($json)-1;$i++)//添加直购商品订单
		{
						$orderStrNo=microtime(true);
						//$orderStrNo=time();
						$sql="INSERT INTO ".$oto."_orderids (`rnd`) VALUES ('{$orderStrNo}')";
						$result=$db->query($sql);
						//$sql="SELECT * FROM ".$oto."_orderids where rnd='{$orderStrNo}'";
						$sql="SELECT * FROM ".$oto."_orderids order by id DESC";
						$result=$db->query($sql);
						while($row=$result->fetch_assoc()){
							$orderids_info[]=$row;//将取得的所有数据赋值给person_info数组
						}
						$orderNo=$orderids_info[0]['id']."".(fmod($orderids_info[0]['id'],7));//订单号
						$shopId=$json[$i]['shops_id'];//商家id
						$orderStatus='-2';//订单状态
						$totalMoney=0;//总消费
						$deliverType=$json[$i]['shops_psway'];//配送方式
						$orderScore=0;//获得积分
						$orderRemarks=$json[$i]['shops_liuyan'];//买家留言
						$requireTime='';//期望送达时间
						$needPay=0;//应付金额
						$youhui_money=0;//优惠价格
						$youhui_id='';
						$sql="INSERT INTO ".$oto."_orders (`orderNo`,`areaId1`,`areaId2`,`areaId3`,`shopId`,`orderStatus`,`totalMoney`,`isSelf`,`userId`,`userName`,`communityId`,`userAddress`,`userPhone`,`orderScore`,`orderRemarks`,`createTime`,`orderunique`,`needPay`,`orderType`,`payType`,`isPay`) VALUES ('$orderNo','$areaId1','$areaId2','$areaId3','$shopId','$orderStatus','$totalMoney','$deliverType','$userId','$userName','$communityId','$userAddress','$userPhone','$orderScore','$orderRemarks','$createTime','$orderunique','$needPay','1','1','0')";
            			$result=$db->query($sql);//插入order表
            			//file_put_contents("tsxx.txt", "\r\n".$sql."执行：".$result."\r\n", FILE_APPEND);
            			$sql="select * from `".$oto."_orders` where `orderNo`='".$orderNo."' and `userId`='{$userId}' and `orderunique`='{$orderunique}'";
						$result=$db->query($sql);//查询刚插进去的orderId
						while($row=$result->fetch_assoc()){
							$order[0]=$row;//将取得的所有数据赋值给person_info数组
						}
						$lin_order_id=$order[0]['orderId'];
						$sql="INSERT INTO ".$oto."_log_orders (`orderId`,`logContent`,`logUserId`,`logType`,`logTime`) VALUES ('$lin_order_id','订单已提交，等待支付','$user_id','0','$createTime')";
            			$result=$db->query($sql);
						//file_put_contents("tsxx.txt", "\r\n".$sql."执行：\r\n", FILE_APPEND);

			for($j=0;$j<count($cart);$j++)
			{
				if($cart[$j]['shopId']==$json[$i]['shops_id'])
				{
					$orderId=$order[0]['orderId'];
					$data[$t]['orderId']=$orderId;
					$sql="INSERT INTO ".$oto."_order_reminds (`orderId`,`userId`,`shopId`,`createTime`) VALUES ('$orderId','$userId','$shopId','$createTime')";
            		$result=$db->query($sql);//插入order_reminds表
					$t++;
					$goodsId=$cart[$j]['goods_id'];
					$goodsNums=$cart[$j]['goods_num'];
					if($cart[$j]['goodss_attr_id']==$cart[$j]['goods_id']."_0_0_0") $goodsPrice=$cart[$j]['shopPrice']+$cart[$i]['shuxing_money'];
					else $goodsPrice=$cart[$i]['shuxing_money'];
					$goodsAttrId=$cart[$j]['goodss_attr_id2'];
					$goodsAttrName=$cart[$j]['goods_attr'];
					$goodsName=$cart[$j]['goodsName'];
					$goodsThums=$cart[$j]['goodsImg'];
					if($cart[$j]['goodss_attr_id']==$cart[$j]['goods_id']."_0_0_0")
					$totalMoney+=($cart[$i]['shopPrice']+$cart[$i]['shuxing_money'])*$cart[$i]['goods_num'];
					else $totalMoney+=($cart[$i]['shuxing_money'])*$cart[$i]['goods_num'];
					//$orderScore+=($cart[$i]['shopPrice']+$cart[$i]['shuxing_money'])*$cart[$i]['goods_num'];
					//$needPay+=($cart[$i]['shopPrice']+$cart[$i]['shuxing_money'])*$cart[$i]['goods_num'];
					
            		$sql="INSERT INTO ".$oto."_order_goods (`orderId`,`goodsId`,`goodsNums`,`goodsPrice`,`goodsAttrId`,`goodsAttrName`,`goodsName`,`goodsThums`,`goodsGroupId`) VALUES ('$orderId','$goodsId','$goodsNums','$goodsPrice','$goodsAttrId','$goodsAttrName','$goodsName','$goodsThums','0')";
            		$result=$db->query($sql);//插入order对应的order_goods表
            		//file_put_contents("tsxx.txt", "\r\n".$sql."执行：".$result."\r\n", FILE_APPEND);
				}
			}
			$shops_id=$json[$i]['shops_id'];
			$sql="SELECT `deliveryFreeMoney`,`deliveryMoney` FROM ".$oto."_shops where shopId='{$shops_id}' LIMIT 1";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$shops_info[$i]=$row;//将取得的所有数据赋值给person_info数组
			}
			$deliveryMoney='0.00';
			if(isset($shops_info[$i]))
			{
				// if($json[$i]['hxiaoji']<$shops_info[$i]['deliveryFreeMoney']) $deliveryMoney=$shops_info[$i]['deliveryMoney'];
				if($totalMoney<$shops_info[$i]['deliveryFreeMoney']) $deliveryMoney=$shops_info[$i]['deliveryMoney'];
			}
			if($json[$i]['shops_psway']==1 || $json[$i]['shops_psway']=='1') $deliveryMoney='0.00';
			$orderScore=intval($json[$i]['hxiaoji']);
			$needPay=$json[$i]['hxiaoji']+$deliveryMoney;
			$youhui_money=(intval($json[$i]['hxiaoji'])-intval($json[$i]['yxiaoji']));
			$youhui_id=$json[$i]['shops_youhui'];
			$sql="update ".$oto."_orders set deliverMoney='{$deliveryMoney}',totalMoney='{$totalMoney}',orderScore='{$orderScore}',needPay='{$needPay}',couponId='{$youhui_id}',couponMoney='{$youhui_money}' where orderId='{$order[0]['orderId']}'";
			$result=$db->query($sql);
			//file_put_contents("tsxx.txt", "\r\n".$sql."执行：".$result."\r\n", FILE_APPEND);
		}
		//清除购物车
        $i=0;
        foreach($car_session as $k=>$v)
        {
            if($car_session[$k]['ischk']==1 || $car_session[$k]['ischk']=='1')
            {
                array_splice($car_session, $i, 1);
                $i--;
            }
            $i++;
        }
		$cart_value=serialize($car_session);
        $sql="update ".$oto."_car_session set car_session='{$cart_value}' where userId='{$user_id}'";
		$result=$db->query($sql);
		//file_put_contents("tsxx.txt", "\r\n".$sql."执行：".$result."\r\n", FILE_APPEND);
		echo json_encode($data);
?>